// 专场模式数据管理
import { get } from '@common/ajax';
import logger from '@common/logger';
import { ClearTip, TipLoading, Tip } from '@common/tip';
import urls from '@common/url';
import { SHOP_COLUMN_LOCATION } from '@constants/shop';
import {
    SET_SPECIAL_SHOP_INFO,
    GET_SPECIAL_GROUPS,
    GET_SPECIAL_SHELF_DATA,
    SET_SPECIAL_FUNCTION_PARAM,
    GET_SPECIAL_COLUMN_DATA,
} from '../mutationType';
const shopSpecial = {
    state: {
        specialShopInfo: null,
        specialGroups: [],
        specialShelfData: [],
        specialColumnData: {},
        // 分页相关操作
        groupId: '',
        pageNumber: 1,
        pageSize: 10,
    },
    mutations: {
        SET_SPECIAL_SHOP_INFO: (state, specialShopInfo) => {
            Object.assign(state, { specialShopInfo });
        },
        GET_SPECIAL_GROUPS: (state, specialGroups) => {
            Object.assign(state, { specialGroups });
        },
        GET_SPECIAL_SHELF_DATA: (state, specialShelfData) => {
            Object.assign(state, { specialShelfData });
        },
        SET_SPECIAL_FUNCTION_PARAM: (state, { pageNumber, groupId }) => {
            Object.assign(state, {
                pageNumber,
                groupId,
            });
        },
        GET_SPECIAL_COLUMN_DATA: (state, specialColumnData) => {
            Object.assign(state, { specialColumnData });
        },
    },
    actions: {
        // 设置专场门店信息
        setSpecialShopInfo: ({ commit }, shopInfo) => {
            logger.info('设置的专场门店信息为：', shopInfo);
            commit(SET_SPECIAL_SHOP_INFO, shopInfo ? { isMainWarehouseInfo: false, ...shopInfo } : null);
        },
        // 获取分组数据
        getSpecialGroups: async ({ commit, state, dispatch }) => {
            TipLoading('加载中...');
            const { specialShopInfo } = state;
            const res = await get(urls.shopGroup, { id: specialShopInfo.isMainWarehouseInfo ? 0 : specialShopInfo.id });
            logger.success('Special => 获取的分组数据为', res.groups);
            commit(GET_SPECIAL_GROUPS, [{
                id: 0,
                name: '全部',
            }, ...res.groups]);
            dispatch('getSpecialShelfData', 0);
        },
        // 获取货架数据
        getSpecialShelfData: async ({ commit, state }, groupId) => {
            const { specialShopInfo, pageSize } = state;
            const param = {
                id: specialShopInfo.isMainWarehouseInfo ? 0 : specialShopInfo.id,
                group_id: groupId,
                query: '',
                page_no: 1,
                page_size: pageSize,
            };
            const res = await get(urls.shopShelf, param);
            commit(SET_SPECIAL_FUNCTION_PARAM, { pageNumber: 1, groupId, });
            commit(GET_SPECIAL_SHELF_DATA, res.prods);
            ClearTip();
        },
        // 获取下一页内容
        getSpecialShelfNextPageData: async ({ commit, state }) => {
            TipLoading('数据获取中...');
            const { specialShopInfo, groupId, pageNumber, pageSize, specialShelfData } = state;
            const pageNo = pageNumber + 1;
            const param = {
                id: specialShopInfo.isMainWarehouseInfo ? 0 : specialShopInfo.id,
                group_id: groupId,
                query: '',
                page_no: pageNo,
                page_size: pageSize,
            };
            const res = await get(urls.shopShelf, param);
            const _result = [...specialShelfData, ...res.prods];
            commit(SET_SPECIAL_FUNCTION_PARAM, { pageNumber: pageNo, groupId, });
            commit(GET_SPECIAL_SHELF_DATA, _result);
            ClearTip();
            res.prods.length === 0 && Tip('已加载全部数据');
        },
        // 获取门店专场栏目
        getSpecialColumnData: async ({ commit, state }) => {
            const { specialShopInfo } = state;
            const param = {
                id: specialShopInfo.isMainWarehouseInfo ? 0 : specialShopInfo.id,
                location: SHOP_COLUMN_LOCATION.index,
            };
            const res = await get(urls.shopColumn, param);
            logger.success('Special => 获取的栏目信息', res);
            commit(GET_SPECIAL_COLUMN_DATA, res);
        },
    },
};

export default shopSpecial;
